package com.hospital.doctor.service;

import com.hospital.doctor.mapper.CheckTableMapper;
import com.hospital.doctor.pojo.CheckBody;
import com.hospital.doctor.pojo.CheckTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckTableServiceImpl implements CheckTableService {

    @Autowired
    private CheckTableMapper checkTableMapper;

    @Override
    public List<CheckTable> selectAll() {
        List<CheckTable> root = checkTableMapper.selectAll();

        List<CheckTable> last = new ArrayList<>();
        //遍历父类
        for (int i = 0; i < root.size(); i++) {
            if (root.get(i).getcFather() == 0){
                last.add(root.get(i));
            }
        }
        //为父类设置子类
        for (CheckTable checkTable : last) {
            checkTable.setChildCheck(getChild(checkTable.getcNumber(),root));
        }
        return last;

    }

    public static List<CheckTable> getChild(int cNumber, List<CheckTable> root){
        //子菜单
        List<CheckTable> childList = new ArrayList<>();
        //遍历所有节点，将父类id与传过来的id比较
        for (CheckTable checkTable : root) {
            if (checkTable.getcNumber() !=0){
                if (checkTable.getcFather() == cNumber){
                    childList.add(checkTable);
                }
            }
        }
        for (CheckTable checkTable : childList) {
            if (checkTable.getcFather()!=0){
                checkTable.setChildCheck(getChild(checkTable.getcNumber(),root));
            }
        }
        //退出递归
        if (childList.size() == 0){
            return null;
        }
        return childList;
    }
}

